package com.yao.amssm.config;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
 * tomcat启动初始化整个应用的类（代替了web.xml）
 * @author yao
 *
 */
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

	// spring应用上下文
	@Override
	protected Class<?>[] getRootConfigClasses() {
		
		return new Class[]{RootConfig.class};
	}

	// springmvc 上下文
	@Override
	protected Class<?>[] getServletConfigClasses() {
		
		return new Class[]{SpringMvcConfig.class};
	}

	// 将DispatcherServlet映射到"/"
	@Override
	protected String[] getServletMappings() {
		
		return new String[]{"/"};
	}
	
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        //Encoding Filter
        FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("encodingFilter",
                new CharacterEncodingFilter());
        encodingFilter.setInitParameter("encoding", "UTF-8");
        encodingFilter.setInitParameter("forceEncoding", "true");
        encodingFilter.addMappingForUrlPatterns(null, false, "/*");
        super.onStartup(servletContext);
    }



}
